package com.aboverock.core.shiro.authz.rest;

import java.util.List;
import javax.servlet.FilterChain;
import org.apache.shiro.web.filter.mgt.DefaultFilter;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.NamedFilterList;
import org.apache.shiro.web.filter.mgt.SimpleNamedFilterList;

/**
 * The Class CustomFilterChainManager.
 *
 * @author Rock Wang
 */
public class RestFilterChainManager extends DefaultFilterChainManager {

    @Override
    protected void addDefaultFilters(boolean init) {
        for (DefaultFilter defaultFilter : DefaultFilter.values()) {
            addFilter(defaultFilter.name(), defaultFilter.newInstance(), init, false);
        }
        addFilter(DefaultFilter.perms.name(), new CustomPermissionsAuthorizationFilter(), false,
                true);
    }

    public FilterChain proxy(FilterChain original, List<String> chainNames) {
        NamedFilterList configured = new SimpleNamedFilterList(chainNames.toString());
        for (String chainName : chainNames) {
            configured.addAll(getChain(chainName));
        }
        return configured.proxy(original);
    }
}
